<template>
  <div id="picture-search-form">
    <a-form layout="inline" :model="searchParams" @finish="doSearch">
      <a-form-item label="关键字" name="searchText">
        <a-input v-model:value="searchParams.searchText" placeholder="从名称和简介搜索" allowClear>
        </a-input>
      </a-form-item>
      <a-form-item label="分类" name="category">
        <a-auto-complete
          v-model:value="searchParams.category"
          style="min-width: 180px"
          :options="categoryOptions"
          placeholder="请输入分类"
          allowClear
        />
      </a-form-item>
      <a-form-item label="标签" name="tags">
        <a-select
          v-model:value="searchParams.tags"
          mode="tags"
          style="min-width: 180px"
          :options="tagsOptions"
          placeholder="请输入标签"
          allowClear
        ></a-select>
      </a-form-item>
      <a-form-item label="日期" name="time">
        <a-range-picker
          style="width: 400px"
          show-time
          v-model:value="dateRange"
          :presets="rangePresets"
          :placeholder="['编辑开始日期', '编辑结束日期']"
          format="YYYY-MM-DD HH:mm:ss"
          @change="onRangeChange"
        />
      </a-form-item>
      <a-form-item label="名称" name="name">
        <a-input v-model:value="searchParams.name" placeholder="请输入名称" allowClear></a-input>
      </a-form-item>
      <a-form-item label="简介" name="introduction">
        <a-input
          v-model:value="searchParams.introduction"
          placeholder="请输入简介"
          allowClear
        ></a-input>
      </a-form-item>
      <a-form-item label="宽度" name="picFormat">
        <a-input-number
          v-model:value="searchParams.picWidth"
          placeholder="请输入宽度"
          allowClear
        ></a-input-number>
      </a-form-item>
      <a-form-item label="高度" name="picFormat">
        <a-input-number
          v-model:value="searchParams.picHeight"
          placeholder="请输入高度"
          allowClear
        ></a-input-number>
      </a-form-item>
      <a-form-item label="格式" name="picFormat">
        <a-input
          v-model:value="searchParams.picFormat"
          placeholder="请输入格式"
          allowClear
        ></a-input>
      </a-form-item>
      <a-form-item>
        <a-space>
          <a-button type="primary" html-type="submit" style="width: 96px"> 搜索</a-button>
          <a-button html-type="reset" style="width: 76px" @click="doClear"> 重置</a-button>
        </a-space>
      </a-form-item>
    </a-form>
  </div>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import {
  listPictureTagCategoryUsingGet
} from '@/request/api/pictureController.ts'
import dayjs from 'dayjs'

interface Props {
  onSearch: (searchParams: API.PictureQueryRequest) => void
}

const props = defineProps<Props>()

const searchParams = reactive<API.PictureQueryRequest>({})

const dateRange = ref([])

const doSearch = () => {
  // 搜索
  props.onSearch?.(searchParams)
}

interface OptionsType {
  value?: string
  label?: string
}

const categoryOptions = ref<Array<OptionsType>>([])
const tagsOptions = ref<Array<OptionsType>>([])

// 获取标签和分类选项
const getCategoryTagsOptions = async () => {
  const { data: res } = await listPictureTagCategoryUsingGet()
  if (res.code === 0 && res.data) {
    // 转换下拉框接受的形式
    categoryOptions.value = (res.data.categoryList ?? []).map((category: string) => {
      return { value: category, label: category }
    })
    tagsOptions.value = (res.data.tagList ?? []).map((tag: string) => {
      return { value: tag, label: tag }
    })
  }
}
const rangePresets = ref([
  { label: 'Last 7 Days', value: [dayjs().add(-7, 'd'), dayjs()] },
  { label: 'Last 14 Days', value: [dayjs().add(-14, 'd'), dayjs()] },
  { label: 'Last 30 Days', value: [dayjs().add(-30, 'd'), dayjs()] },
  { label: 'Last 90 Days', value: [dayjs().add(-90, 'd'), dayjs()] },
])
// 日期发生改变时
const onRangeChange = (dates: any[], dateStrings: string[]) => {
  if (dates.length < 2) {
    searchParams.startEditTime = undefined
    searchParams.endEditTime = undefined
  } else {
    searchParams.startEditTime = dates[0].toDate()
    searchParams.endEditTime = dates[1].toDate
  }
}
// 重置表单
const doClear = () => {
  // 重置表单
  Object.keys(searchParams).forEach((key) => {
    searchParams[key as keyof API.PictureQueryRequest] = undefined
  })
  dateRange.value = []
  // 通知父组件,并将改变后参数传过去
  props.onSearch(searchParams)
}
// 页面加载时调用
onMounted(() => {
  getCategoryTagsOptions()
})
</script>
<style scoped lang="less">
#picture-search-form {
  .ant-form-item {
    margin-top: 16px;
  }
}
</style>
